package testWeb;

import java.io.IOException;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import conn.Connection1;



/**
 * Servlet implementation class registerServlet
 */
@WebServlet(description = "registerServlet", urlPatterns = { "/registerServlet" })
public class registerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public registerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Connection1 conn =new Connection1();
		com.mysql.jdbc.Connection connection = conn.getConnection();
		String username = new String(request.getParameter("username"));
		String pwd = new String(request.getParameter("password"));
		String userType = new String(request.getParameter("userType"));
		String sql = "insert into user(name,password,userType) values(?,?,?)";
		try {
			PreparedStatement ps = ((java.sql.Connection) connection).prepareStatement(sql);
			ps.setString(1,username);
			ps.setString(2, pwd);
			ps.setString(3,userType);
			int row = ps.executeUpdate();
			if(row > 0){
				HttpSession session = request.getSession();
				session.setAttribute("username",username);
				session.setMaxInactiveInterval(5*60);
				request.getRequestDispatcher("indexFront.jsp").forward(request, response);
			}
			ps.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
